<?php
$dsn        = 'mysql:dbname=multimedian-wga;host=127.0.0.1';
$user       = 'multimedian-wga';
$password   = 'Kakkerlak';

define( 'SEARCH_PATH', 'application/models;library');

define( 'MAX_MOVIES', 10 );         //maximum movies to generate
define( 'MAX_ANNOTATIONS', 25 );    //maximum annotations to generate

define( 'CHANNEL_SINGLE', 1 );
define( 'CHANNEL_MULTI', 2 );
define( 'CHANNEL_COMMENT', 5 );

try{
    $db = new PDO($dsn, $user, $password);
}
catch( PDOException $e ){
    echo 'Connection failed: ' . $e->getMessage();
}


function base_path( $append = NULL ){
	static $path = null;

	if( null == $path ){
		$path = str_replace( sprintf( '/scripts/%s', basename( __file__ ) ), '',  __file__ );
	}

	if( func_num_args() > 0 ){
		$args = func_get_args();

		return $path . '/' . join( '/', $args );
	}

	return $path;
}

function random_lipsum(){
	static $lipsum = null;

	if( null == $lipsum ){
		$lipsum = file_get_contents( 'loremipsum.txt' );
		$lipsum = wordwrap( $lipsum, 100 );
		$lipsum = array_filter( explode( "\n", $lipsum ) );
	}


	return $lipsum[rand(0, count($lipsum) - 1)];
}

function make_annotation( $mid, $cid, $uid, $type, $max_time ){
	global $db;

	$duration = rand( 0, 30 );
	$time     = rand( 0, $max_time - $duration );

	$annotation = array(
		'movie_id'		=> $mid,
		'channel_id'	=> $cid,
		'user_id'		=> $uid,
		'comment'		=> '\'\'',
		'duration'		=> $duration,
		'time'			=> $time
	);

	$comment  = null;
	$keywords = array();


	switch( intval($type) ){
		case CHANNEL_MULTI:
			$qh 		= $db->query(sprintf('SELECT * FROM keyword LEFT JOIN channel_keyword ON channel_keyword.keyword_id = keyword.id WHERE channel_id = %d', $cid ));
			$keywords	= $qh->fetchAll( PDO::FETCH_OBJ );
			$num 		= rand( 1, count( $keywords ) );
			$keywords   = array_slice( $keywords, 0, $num );


			break;
		case CHANNEL_SINGLE:
			$qh 		= $db->query(sprintf('SELECT * FROM keyword LEFT JOIN channel_keyword ON channel_keyword.keyword_id = keyword.id WHERE channel_id = %d ORDER BY RAND() LIMIT 1', $cid ));
			$keywords 	= $qh->fetchAll( PDO::FETCH_OBJ );
			break;
		case CHANNEL_COMMENT:
			$annotation['comment']	= sprintf("'%s'", random_lipsum());
			break;
	}

	$query = 'INSERT INTO annotation (' . join(',', array_keys( $annotation )) .  ') VALUES (' . join(',', $annotation ) . ')';

	$db->query($query);
	$aid = $db->lastInsertId();


	foreach( $keywords as $keyword ){
		$db->query(sprintf('INSERT INTO annotation_keyword (annotation_id, keyword_id ) VALUES (%d, %d)', $aid, $keyword->id ));
	}
}

set_include_path( base_path('library') );
include base_path('library', 'Zend', 'Loader', 'Autoloader.php');
include base_path('library', 'Zend', 'Application', 'Module', 'Autoloader.php');

$qh = $db->query( 'SELECT * FROM movie LEFT JOIN video_info ON video_info.movie_id = movie_id GROUP BY movie.id' );
$movies = $qh->fetchAll( PDO::FETCH_OBJ );


$qh = $db->query( 'SELECT * FROM user_openid');
$users = $qh->fetchAll( PDO::FETCH_OBJ );

$count = 0;

foreach( $movies as $movie ){
	$qh = $db->query( sprintf('SELECT * FROM channel LEFT JOIN channel_movie ON movie_id = %d', $movie->id ) );
	$channels = $qh->fetchAll( PDO::FETCH_OBJ );
	foreach( $channels as $channel ){
		foreach( $users as $user ){
			make_annotation( $movie->id, $channel->id, $user->id, $channel->type, $movie->duration );
			$count++;
		}
	}
}

echo sprintf( "Generated %d annotations for %d movies and %d users\n", $count, count($movies), count($users) );
